#include "XXXMsgManager.h"
#include "MsgHandler/ConfigMsgHandler.h"
#include "MsgHandler/CtrlMsgHandler.h"
#include "MsgHandler/SysStatusMsgHandler.h"
#include "MsgReporter/StatusMsgReporter.h"
#include "MsgReporter/MotorMsgReporter.h"
#include "MsgReporter/IOMsgReporter.h"
#include "MsgReporter/ImuMsgReporter.h"

#include <iostream>

XXXMsgManager::XXXMsgManager(const std::string& name) : BaseManager(name)
{
}

XXXMsgManager::~XXXMsgManager()
{
    m_ReportTimer.RemoveAllTimer(); 
    Destroy();
    m_initSuccess = false;
}

bool XXXMsgManager::Init()
{
    InitMsgHandler();       // 初始化响应消息处理者
    InitMsgReporter();      // 初始化消息的上报者

    m_initSuccess = true;
    std::cout << "XXXMsgManager::Init() success" << std::endl;
    return true;
}

void XXXMsgManager::InitMsgHandler()
{
    m_configMsgHandlerPtr = std::make_shared<ConfigMsgHandler>();

    RegisterMsgHandler<CtrlMsgHandler>();
    RegisterMsgHandler<SysStatusMsgHandler>();

    for (auto& item : m_msgHandlerMap)
    {
        item.second->Init();
    }
}


void XXXMsgManager::InitMsgReporter()
{
    RegisterMsgReporter<StatusMsgReporter>();
    RegisterMsgReporter<MotorMsgReporter>();
    RegisterMsgReporter<ImuMsgReporter>();
    RegisterMsgReporter<IOMsgReporter>();

    for (auto& item : m_msgReporterMap)
    {
        item.second->Init();
    }
}

void XXXMsgManager::Reset()
{
    m_ReportTimer.RemoveAllTimer();
    m_configSuccess = false;
}

void XXXMsgManager::Destroy()
{
    m_msgHandlerMap.clear();
    m_msgReporterMap.clear();
}


bool XXXMsgManager::IsConfigSuccess()
{
    return m_configSuccess;
}

bool XXXMsgManager::HandleMsg(const std::string& msg)
{
    if (!m_initSuccess)
    {
        std::cout << "msg manager is not init" << std::endl;
        return false;
    }

    if (msg.empty())
    {
        std::cout << "msg is empty" << std::endl;
        return false;
    }


// 处理客户端的第一个消息，必须是config消息包. 而且允许多次下发config
    std::string msgType = GetMsgType(msg);
    if (msgType == "config")       
    {
        return HandleConfigMsg(msg);
    }
// 处理客户端的其他消息     m_configSuccess配置成功后，才能进行其他消息的请求和处理                                                
    else if (m_configSuccess)
    {
        auto it = m_msgHandlerMap.find(msgType);
        if (it!= m_msgHandlerMap.end())
        {
            return it->second->HandleMsg(msg);
        }
        else
        {
            std::cout << "msg head not found. msgType:" << msgType << std::endl;
            return false;
        }
    }
    return false;
}

bool XXXMsgManager::HandleConfigMsg(const std::string& msg)
{
    bool configSuccess = m_configMsgHandlerPtr->HandleMsg(msg);
    if (!configSuccess)
    {
        std::cout << "handle config msg failed. " << std::endl;
        return false;
    }

    // TODO:处理配置消息请求

    SetReportTimer();                                               // 注册消息上报者的定时器

    m_configSuccess = true;
    return true;
}

void XXXMsgManager::SetReportTimer()
{
    m_ReportTimer.RemoveAllTimer();                                 // 先清空定时器
    
    for (auto& it : m_msgReporterMap)
    {
        TimerData timerData;
        timerData.id = it.second->GetMsgType();
        timerData.timeInterval = it.second->GetTimeInterval();
        timerData.func = std::bind(&XXXMsgManager::ReportMsg, this, it.second);
        timerData.countl = 0;

        m_ReportTimer.AddTimer(timerData);
    }
}

void XXXMsgManager::SetSessionPtr(SessionPtr sessionPtr)
{
    m_sessionPtr = sessionPtr;
}

bool XXXMsgManager::SendMsg(const std::string& msg)
{
    if (m_sessionPtr != nullptr)
    {
        m_sessionPtr->Send(msg);
        return true;
    }
    return false;
}

void XXXMsgManager::ReportMsg(BaseMsgReporterPtr& msgReporterPtr)
{
    std::string reportMsg = msgReporterPtr->GetReportMsg();
    SendMsg(reportMsg);
}

// 
std::string XXXMsgManager::GetMsgType(const std::string& msg)
{
    std::string msgType;
    // msgType = ParseMsgType(msg);    TODO: 获取消息类型
    return msgType;
}
